package com.bms.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.bms.common.JsonDateSerializer;

/**
 * 实体类：管理员
 * 
 * @author Lan
 */
@Entity
@Table(name = "T_USER")
public class User extends BaseEntity {

	private static final long serialVersionUID = 6798491716161759870L;

	private String username;// 用户名
	private String password;// 密码
	private String chineseName; // 中文名
//	private Role role;//多对一
	private Set<Role> roleSet = new HashSet<Role>(); //用户可拥有多种角色
	
	private String phone;
	private String email;
	private String description;
	
	private Boolean isEnabled;// 账号是否启用（注销）
	private Date loginDate;// 最后登录日期
	private String loginIp;// 最后登录IP

	private String newPassword;//新密码
	private String confirmNewPwd;//确认新密码
	
	public User() {
	}

	public User(String username, String password, String chineseName,
			Set<Role> roleSet, Boolean isEnabled) {
		this.username = username;
		this.password = password;
		this.chineseName = chineseName;
		this.roleSet = roleSet;
		this.isEnabled = isEnabled;
	}

	/**
	 * 用户名（唯一，不为空）
	 * 
	 * @author Lan
	 * @return
	 */
	@Column(updatable = false, nullable = false, unique = true, length = 20)
	public String getUsername() {
		return username;
	}

	/**
	 * 用户名（唯一，不为空）
	 * 
	 * @author Lan
	 * @param username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 密码（不为空）
	 * 
	 * @author Lan
	 * @return
	 */
	@Column(nullable = false, length = 32)
	public String getPassword() {
		return password;
	}

	/**
	 * 密码（不为空）
	 * 
	 * @author Lan
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 中文名
	 * 
	 * @author Lan
	 * @return chineseName
	 */
	@Column(length = 20)
	public String getChineseName() {
		return chineseName;
	}

	/**
	 * 中文名
	 * 
	 * @author Lan
	 * @param chineseName
	 */
	public void setChineseName(String chineseName) {
		this.chineseName = chineseName;
	}

	/*@ManyToOne
	@JoinColumn(name="roleID")
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}*/
	
	@ManyToMany //多对多关联 User是主的一方 Role是附属的一方
	@JoinTable(name="T_USER_ROLE", //指定中间表表名
		joinColumns={@JoinColumn(name="userID")},//本类主键在中间表生成的对应字段名
		inverseJoinColumns={@JoinColumn(name="roleID")}//对方类主键在中间表生成的对应字段名
	)
	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	@Column(length = 20)
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(length = 100)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(length = 200)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 账号是否启用
	 * 
	 * @author Lan
	 * @return true启用；false未启用（注销）
	 */
	@Column(nullable = false)
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	/**
	 * 账号是否启用
	 * 
	 * @author Lan
	 * @param isEnabled
	 *            true启用；false未启用（注销）
	 */
	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	/**
	 * @author Lan
	 * @return 最后登录日期
	 */
	@JsonSerialize(using = JsonDateSerializer.class)
	public Date getLoginDate() {
		return loginDate;
	}

	/**
	 * 最后登录日期
	 * 
	 * @author Lan
	 * @param loginDate
	 */
	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	/**
	 * @author Lan
	 * @return 最后登录IP
	 */
	@Column(length = 20)
	public String getLoginIp() {
		return loginIp;
	}

	/**
	 * @author Lan
	 * @param 最后登录IP
	 */
	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	@Transient
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	@Transient
	public String getConfirmNewPwd() {
		return confirmNewPwd;
	}

	public void setConfirmNewPwd(String confirmNewPwd) {
		this.confirmNewPwd = confirmNewPwd;
	}
}
